package com.zhenghaijin.searchservice.task;

import com.zhenghaijin.searchservice.model.Anime;
import com.zhenghaijin.searchservice.service.AnimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;

@Component
public class AnimeSyncTask {

    @Autowired
    private RestTemplate restTemplate; 

    @Autowired
    private AnimeService animeService;

    // 定时任务：每小时同步一次（cron 表达式可根据需要调整）
    @Scheduled(cron = "0 0 * * * ?")
    public void syncAnimeData() {
        // 从动漫服务接口获取数据
        String url = "http://anime-service/anime/list"; // 假定动漫服务地址
        ResponseEntity<Anime[]> response = restTemplate.getForEntity(url, Anime[].class);
        Anime[] animeArray = response.getBody();
        if (animeArray != null) {
            List<Anime> animeList = Arrays.asList(animeArray);
            // 调用服务层同步数据
            animeService.syncAnimeData(animeList);
        }
    }
}
